﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public abstract class BattleInfoUI : AlphaShowHideUI
{
    protected Vector3 m_Offset;

    // Use this for initialization
    protected virtual void Start () {
	}

    protected Transform m_Reference;
    protected Func<Vector3, Vector3, Vector3> m_WorldToUIPosFunc;
    public virtual void Init(Transform refer, Func<Vector3, Vector3, Vector3> worldToUIPosFunc, Vector3 offset = new Vector3())
    {
        m_Offset = offset;
        m_Reference = refer;
        m_WorldToUIPosFunc = worldToUIPosFunc;
        
        var rect = GetComponent<RectTransform>();
        SetPosition(rect, refer.transform.position);
    }

    protected void SetPosition(RectTransform rect, Vector3 uipos)
    {
        rect.SetUIPosition(uipos);
        //rect.transform.position = uipos;
        //var ap = rect.anchoredPosition3D;
        //ap.z = 10;
        //rect.anchoredPosition3D = ap;
    }

    // Update is called once per frame
    protected virtual void Update () {

        if(m_WorldToUIPosFunc != null && m_Reference)
        {
            SetPosition(GetComponent<RectTransform>(), m_WorldToUIPosFunc(m_Reference.TransformPoint(m_Offset), Vector3.zero));
        }

	}
}
